package com.zygame.sdk.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager.LayoutParams;

import com.zygame.sdk.CmCode;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.utils.phone.Phoneuitl;
import com.zygame.sdk.utils.res.ResourceUtil;


//public abstract class BaseDialog extends Dialog implements OnDismissListener{
public abstract class BaseDialog extends Dialog implements OnDismissListener,OnKeyListener{
		
	public Context mContext;
	private int mWidth;
	private int mHeight;
	
	@SuppressLint("NewApi") public BaseDialog(Context context){
		super(context,ResourceUtil.getStyleId(context, "actionSheet"));
		
		this.setCanceledOnTouchOutside(false);
		this.mContext = context;
		this.setCancelable(false);
		
		int w = Phoneuitl.getWpixels(context);
		int h = Phoneuitl.getHpixels(context);
		if (ZyGameSDK.getInstance().getOrientation()) {   //横屏
			this.mWidth = 1400/3;
			this.mHeight = 870/3;
		}else{
			
			if((450 < w && w < 500) && (h > 800 && h < 900)){    // 480x854
				this.mWidth = 300;
//				this.mHeight = (int)(mWidth * 0.618)+60;
				this.mHeight = (int)(mWidth * 0.618)+100;
			}else{
				this.mWidth = 360;
//				this.mHeight = (int)(mWidth * 0.618) + 30;
				this.mHeight = (int)(mWidth * 0.618) + 70;
			}
		}
//		int arr[] = Phoneuitl.getScreenWidthHeight((Activity)context);
//		System.out.println("width:height = " + arr[0] + "---" +arr[1]);
//		System.out.println("mHeight = " + this.mHeight + "   this.mWidth = " + this.mWidth);
		
	}
	
	public BaseDialog(Context context,int width ,int height){
		super(context,ResourceUtil.getStyleId(context, "actionSheet"));
		this.setCanceledOnTouchOutside(false);
		this.mContext = context;
		this.mWidth = width;
		this.mHeight = height;
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		subOnCreate();
		  
		LayoutParams params = this.getWindow().getAttributes();
		float density = mContext.getResources().getDisplayMetrics().density;
		
		params.width = (int) (density*mWidth);
		params.height = (int) (density*mHeight);
		
		this.setOnDismissListener(this);
		this.setOnKeyListener(this);
	}


	@Override
	public void show() {
		super.show();
	}
	
	protected abstract void subOnCreate();
	
	protected void setDismiss(){
		this.dismiss();
	}

//	@Override
//	public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
//		return false;
//	}

	@Override
	public void onDismiss(DialogInterface arg0) {
		int flag = ZyGameSDK.getInstance().dialogDismissFlag;
//		switch (flag) {
//		case CmCode.CODE_LOGIN_CANCEL:
//			CmGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_CANCEL, "65sdk login cancel");
//			break;
//		case CmCode.CODE_LOGIN_FAIL:
//			CmGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_FAIL, "65sdk login fail");
//			break;
//		case CmCode.CODE_REGISTER_CANCEL:
//			CmGameSDK.getInstance().onResult(CmCode.CODE_REGISTER_CANCEL, "65sdk register_cancel");
//			break;
//		case CmCode.CODE_REGISTER_FAIL:
//			CmGameSDK.getInstance().onResult(CmCode.CODE_REGISTER_CANCEL, "65sdk register_fail");
//			break;
//		default:
//			break;
//		}
//		CmGameSDK.getInstance().setDialogDismissFlag(0);
	}

	@Override
	public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
//		CmGameSDK.getInstance().onResult(CmCode.CODE_LOGIN_CANCEL, "65sdk login cancel");
		return false;
	}
	
	
}
